//
//  MathTool.swift
//  AreaMeasureDevice
//
//  Created by iOS on 2021/5/21.
//

import Foundation

class MathTool:NSObject {

    /// 精确乘法运算
    /// - Parameters:
    ///   - x: 被乘数
    ///   - y: 乘数
    /// - Returns: 两个参数积
    class func multiply(x:Double, y:Double) -> Double {
        let dx = Decimal(string: String(x))
        let dy = Decimal(string: String(y))
        let value = dx! * dy!
        return Double(truncating: value as NSNumber)
    }


    /// 精确加法运算
    /// - Parameters:
    ///   - x: 被加数
    ///   - y: 加数
    /// - Returns: 参数和
    class func add(x:Double, y:Double) -> Double {
        let dx = Decimal(string: String(x))
        let dy = Decimal(string: String(y))
        let value = dx! + dy!
        return Double(truncating: value as NSNumber)
    }


    /// 精确减法运算
    /// - Parameters:
    ///   - x: 被减数
    ///   - y: 减数
    /// - Returns: 参数差
    class func subtract(x:Double, y:Double) -> Double {
        let dx = Decimal(string: String(x))
        let dy = Decimal(string: String(y))
        let value = dx! - dy!
        return Double(truncating: value as NSNumber)
    }


    /// 精确除法运算
    /// - Parameters:
    ///   - x: 被除数
    ///   - y: 除数
    /// - Returns: 参数商
    class func divide(x:Double, y:Double) -> Double {
        let dx = Decimal(string: String(x))
        let dy = Decimal(string: String(y))
        let value = dx! / dy!
        return Double(truncating: value as NSNumber)
    }

    class func LatAnalysis(Lat:String, NS:String) -> Double {
        let latZS = Int(Lat[0..<2])
        let latXS = divide(x: Double(Lat[0..<2])!, y: 60).roundTo(places: 10)
        var latTemp = add(x: Double(latZS!), y: latXS)
        if NS == "N" {
            let n_mode:Double = 1
            latTemp = multiply(x: latTemp, y: n_mode)
        }
        if NS == "S" {
            let s_mode:Double = -1
            latTemp = multiply(x: latTemp, y: s_mode)
        }
        return latTemp
    }

    class func LngAnalysis(Lng:String, EW:String) -> Double {
        let lngZS = Int(Lng[0..<3])
        let lngXS = divide(x: Double(Lng[0..<3])!, y: 60).roundTo(places: 10)
        var lngTemp = add(x: Double(lngZS!), y: lngXS)
        if EW == "E" {
            let n_mode:Double = 1
            lngTemp = multiply(x: lngTemp, y: n_mode)
        }
        if EW == "W" {
            let s_mode:Double = -1
            lngTemp = multiply(x: lngTemp, y: s_mode)
        }
        return lngTemp
    }

    class func LatAnalysis_Du(lat:Double) -> Int {
        let tem = String(lat)
        let lat_du = Int(tem[0..<2])
        return lat_du!
    }


}
